<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-9-06
 * Time: 下午22:31
 * To change this template use File | Settings | File Templates.
 */
class AdTypesDB  extends DBReader{
    //根据ad_type_id 获取ad_type 信息
    public function get($ad_type_id){

        $sql = 'select ad_type_id, type_name, left_value, right_value, parent_ad_type_id from ad_types where ad_type_id = :ad_type_id';

        return $this->db()->getRow($sql,array('ad_type_id'=>$ad_type_id));
    }

    //获取ad_types 列表<带分页>
    public function getAdTypeList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['type_name'])){
            $where .= ' and type_name like :type_name';
            $bindarr['type_name'] = '%'.$filter['type_name'].'%';
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(ad_type_id) from ad_types where ' . $where , $bindarr)
            ->rs('select ad_type_id, type_name, left_value, right_value, parent_ad_type_id from ad_types where '.$where.' order by left_value asc',$bindarr);

        return $ret;
    }

    //刷新分类树的预排序
    public function refreshAdTypeTree($right_value,$span = '+2'){
        $lsql = "UPDATE ad_types SET left_value=left_value".$span." WHERE left_value > :right_value";
        $rsql = "UPDATE ad_types SET right_value=right_value".$span." WHERE right_value >= :right_value";
        $this->db()->execute($lsql,array('right_value'=>$right_value));
        $this->db()->execute($rsql,array('right_value'=>$right_value));
        return true;
    }

    //由于树的顶部是虚拟的：left_value:0,right_value:max_value
    public function getMaxRightValue(){

        $sql = "select max(right_value) from ad_types";
        return $this->db()->getOne($sql);
    }

    public function addAdType($type_name,$parent_ad_type_id){

        if($parent_ad_type_id){
            $ad_type_info = $this->get($parent_ad_type_id);
            $left_value = $ad_type_info['right_value'];
            $right_value = $ad_type_info['right_value']+1;
            $this->refreshAdTypeTree($ad_type_info['right_value']);
        }else{
            $max_value = $this->getMaxRightValue();
            $left_value = $max_value + 1;
            $right_value = $max_value + 2;
        }

        $sql = "insert into ad_types(type_name,left_value,right_value,parent_ad_type_id) values(:type_name,:left_value,:right_value,:parent_ad_type_id)";

        $bindarr = array(
            'type_name'=>$type_name,
            'left_value'=>$left_value,
            'right_value'=>$right_value,
            'parent_ad_type_id'=>$parent_ad_type_id
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //根据左右值获取子分类，包含本身
    public function getAdTypeByLR($left_value,$right_value){

        $sql = 'select ad_type_id, type_name, left_value, right_value, parent_ad_type_id from ad_types where left_value >= :left_value and right_value <= :right_value';

        return $this->db()->getAll($sql,array('left_value'=>$left_value,'right_value'=>$right_value));
    }

    public function delAllAdType($ad_type_id){

        $ad_type_info = $this->get($ad_type_id);

        $v_width = $ad_type_info['right_value'] - $ad_type_info['left_value'] + 1;

        $rs = $this->db()->execute("DELETE FROM ad_types WHERE left_value BETWEEN :left_value AND :right_value",array('left_value'=>$ad_type_info['left_value'],'right_value'=>$ad_type_info['right_value']));

        $this->db()->execute("UPDATE ad_types SET left_value = left_value - :v_width WHERE left_value > :right_value",array('v_width'=>$v_width,'right_value'=>$ad_type_info['right_value']));

        $this->db()->execute("UPDATE ad_types SET right_value = right_value - :v_width WHERE right_value > :right_value",array('v_width'=>$v_width,'right_value'=>$ad_type_info['right_value']));

        return $rs->rowCount();
    }

    public function updateAdType($ad_type_id,$type_name,$parent_ad_type_id = null){
        $set_str = '';
        if($parent_ad_type_id){
            $set_str = ',parent_ad_type_id = :parent_ad_type_id';
        }

        $sql = "update ad_types set type_name = :type_name ".$set_str." where ad_type_id = :ad_type_id";

        $bindarr = array(
            'type_name'=>$type_name,
            'ad_type_id'=>$ad_type_id
        );

        if($parent_ad_type_id){
            $bindarr['parent_ad_type_id'] = $parent_ad_type_id;
        }

        return $this->db()->execute($sql,$bindarr);
    }

    //获取用户拥有权限的菜单树
    public function getTree($ad_type_id = 0,$filter_child = false){

        if($ad_type_id){//查询单个树节点
            $sql = "select left_value,right_value from ad_types where ad_type_id = :ad_type_id";
            $root_info = $this->db()->getRow($sql,array('ad_type_id'=>$ad_type_id));
            $sql = 'select ad_type_id, type_name, left_value, right_value, parent_ad_type_id from ad_types where left_value >= :left_value and right_value <= :right_value';

            $tree_info = $this->db()->getAll($sql,array('left_value'=>$root_info['left_value'],'right_value'=>$root_info['right_value']));
        }else{//查询所有树节点
            $where = 'where 1=1';

            if($filter_child){
                //$where .= ' and right_value-left_value > 1';
                $where .= ' and parent_ad_type_id = 0';
            }

            $sql = 'select ad_type_id, type_name, left_value, right_value, parent_ad_type_id from ad_types '.$where.' order by left_value asc';

            $tree_info = $this->db()->getAll($sql);
        }

        return $tree_info;

    }

}